package cn.iocoder.an.module.mold.controller.admin.repairaccount;

import cn.iocoder.an.framework.common.exception.ServiceException;
import cn.iocoder.an.module.mold.controller.admin.upkeeptobe.vo.UpkeepTobeDispatchVO;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.repairaccount.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.repairaccount.RepairAccountDO;
import cn.iocoder.an.module.mold.service.repairaccount.RepairAccountService;

@Tag(name = "管理后台 - 模具维修申请")
@RestController
@RequestMapping("/mold/repair-account")
@Validated
public class RepairAccountController {

    @Resource
    private RepairAccountService repairAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建模具维修申请")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:create')")
    public CommonResult<Long> createRepairAccount(@Valid @RequestBody RepairAccountSaveReqVO createReqVO) {
        return success(repairAccountService.createRepairAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具维修申请")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:update')")
    public CommonResult<Boolean> updateRepairAccount(@Valid @RequestBody RepairAccountSaveReqVO updateReqVO) {
        repairAccountService.updateRepairAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具维修申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:repair-account:delete')")
    public CommonResult<Boolean> deleteRepairAccount(@RequestParam("id") Long id) {
        repairAccountService.deleteRepairAccount(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具维修申请")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:delete')")
    public CommonResult<Boolean> deleteRepairAccountList(@RequestParam("ids") List<Long> ids) {
        repairAccountService.deleteRepairAccountListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具维修申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:query')")
    public CommonResult<RepairAccountRespVO> getRepairAccount(@RequestParam("id") Long id) {
        RepairAccountDO repairAccount = repairAccountService.getRepairAccount(id);
        return success(BeanUtils.toBean(repairAccount, RepairAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具维修申请分页")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:query')")
    @TransMethodResult
    public CommonResult<PageResult<RepairAccountRespVO>> getRepairAccountPage(@Valid RepairAccountPageReqVO pageReqVO) {
        PageResult<RepairAccountDO> pageResult = repairAccountService.getRepairAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具维修申请 Excel")
    @PreAuthorize("@ss.hasPermission('mold:repair-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairAccountExcel(@Valid RepairAccountPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairAccountDO> list = repairAccountService.getRepairAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具维修申请.xls", "数据", RepairAccountRespVO.class,
                BeanUtils.toBean(list, RepairAccountRespVO.class));
    }


    @PutMapping("/dispatch")
    @Operation(summary = "派工接口")
    @TransMethodResult
    public CommonResult<Boolean> dispatch(@Valid @RequestBody RepairAccountDispatchVO dispatchVO) {
        return success(repairAccountService.dispatch(dispatchVO));
    }

    @PutMapping("/cancel-dispatch")
    @Operation(summary = "撤销派工接口")
    @TransMethodResult
    public CommonResult<Boolean> cancelDispatch(@RequestParam("id") Long id) {
        try {
            return success(repairAccountService.cancelDispatch(id));
        } catch (ServiceException e) {
            return CommonResult.error(e.getCode(), e.getMessage());
        }
    }

    @PutMapping("/audit")
    @Operation(summary = "审核接口")
    @TransMethodResult
    public CommonResult<Boolean> audit(@Valid @RequestBody RepairAccountAuditVO auditVO) {
        try {
            return success(repairAccountService.audit(auditVO));
        } catch (ServiceException e) {
            return CommonResult.error(e.getCode(), e.getMessage());
        }
    }
}